package com.sdw.message.dao.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;

public class BaseDataSource implements DataSource{
	static Logger _logger = Logger.getLogger(BaseDataSource.class);
	
	private final DataSource dataSource;
	
	public BaseDataSource(String filePath){
		dataSource = this.getDataSource(filePath);
	}
	
	private DataSource getDataSource(String filePath){				
    	InputStream input = null;
    	Properties properties = new Properties();
    	DataSource ds = null;
		try {
			File cfgFile = ResourceUtils.getFile(filePath);
			
			input = new FileInputStream(cfgFile);
			properties.loadFromXML(input);
			ds = BasicDataSourceFactory.createDataSource(properties);
			
			return ds;
		} catch (InvalidPropertiesFormatException e) {
			String message = String.format("path:database.xml file io exception.");
			_logger.error(message, e);
			throw new DataBaseException(message, e);
		} catch (IOException e) {
			String message = String.format("path:database.xml data create fail.");
			_logger.error(message, e);
			throw new DataBaseException(message, e);
		}catch (Exception e) {
			String message = String.format("path:database.xml data create fail.");
			_logger.error(message, e);
			throw new DataBaseException(message, e);
		}finally{
			try{
				if(input != null){
					input.close();
				}
			}catch(IOException e){
				String message = String.format("path:database.xml data create fail.");
				_logger.error(message, e);
				throw new DataBaseException(message, e);
			}
		}
		
    }

	@Override
        public PrintWriter getLogWriter() throws SQLException {
	        return this.dataSource.getLogWriter();
        }
	@Override
        public void setLogWriter(PrintWriter out) throws SQLException {
	        this.dataSource.setLogWriter(out);
        }
	@Override
        public void setLoginTimeout(int seconds) throws SQLException {
	        this.dataSource.setLoginTimeout(seconds);
        }
	@Override
        public int getLoginTimeout() throws SQLException {
	        return this.dataSource.getLoginTimeout();
        }
	@Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
	        return this.dataSource.unwrap(iface);
        }
	@Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
	        return this.dataSource.isWrapperFor(iface);
        }
	@Override
        public Connection getConnection() throws SQLException {
	        return this.dataSource.getConnection();
        }
	@Override
        public Connection getConnection(String username, String password)
                        throws SQLException {
	        return this.dataSource.getConnection(username, password);
        }

}
